package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.qu_tao.R;
import com.example.qu_tao.provider.TQProvider;

import java.util.regex.Pattern;

public class tixianmimaActivity extends AppCompatActivity {

    private EditText phoneNumberInput, newPasswordInput, confirmPasswordInput;
    private Button changePasswordButton;
    private ImageView backButton;
    private long userId;
    private String userPhone;

    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tixianmima);

        // 获取当前登录用户的ID
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        userId = sharedPreferences.getLong("user_id", -1);

        // 获取用户的手机号
        if (userId != -1) {
            Uri uri = Uri.parse("content://com.example.qu_tao.provider/user/" + userId);
            Cursor cursor = getContentResolver().query(uri, new String[]{"user_phone"}, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                userPhone = cursor.getString(cursor.getColumnIndex("user_phone"));
                cursor.close();
            }
        }

        phoneNumberInput = findViewById(R.id.phone_number_input);
        newPasswordInput = findViewById(R.id.new_withdraw_password_input);
        confirmPasswordInput = findViewById(R.id.confirm_new_withdraw_password_input);
        changePasswordButton = findViewById(R.id.change_withdraw_password_button);
        backButton = findViewById(R.id.ChangePassword_back_button);

        changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputPhone = phoneNumberInput.getText().toString();
                String newPassword = newPasswordInput.getText().toString();
                String confirmPassword = confirmPasswordInput.getText().toString();

                if (!inputPhone.equals(userPhone)) {
                    Toast.makeText(tixianmimaActivity.this, "手机号与当前登录信息不匹配", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!isValidPassword(newPassword)) {
                    Toast.makeText(tixianmimaActivity.this, "新密码必须为6位数字", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!newPassword.equals(confirmPassword)) {
                    Toast.makeText(tixianmimaActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                Toast.makeText(tixianmimaActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean isValidPassword(String password) {
        String regex = "^[0-9]{6}$";
        return Pattern.matches(regex, password);
    }
}